package com.dfec.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dfec.book.constant.SexEnum;
import com.dfec.book.dao.StudentDao;
import com.dfec.book.entity.StudentEntity;
import com.dfec.book.exception.ServiceException;
import com.dfec.book.service.StudentService;
import com.dfec.book.util.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author tangrg
 * @email 1446232546@qq.com
 * @date 2025-07-2025/7/2 10:59:01
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> implements StudentService {

//    @Resource


    private static QueryWrapper<StudentEntity> buidQueryWrap(StudentEntity student) {
        QueryWrapper<StudentEntity> qw = new QueryWrapper<>();
        if (StringUtils.isNotBlank(student.getName())) {
            qw.like("name", student.getName());
        }


        return qw;
    }

    private static void checkParm(StudentEntity student) {
        if (StringUtils.isBlank(student.getName())) {
            throw new ServiceException("学生姓名不能为空");
        }
        /**
         * 判断性别
         * 性别 0、女 1、男 2、未知
         * 判断学生性别是否为空
         * throw new ServiceException("学生性别非法");
         */
        if (!Objects.isNull(student.getSex())) {
            SexEnum sexEnum = SexEnum.of(student.getSex());
            if (sexEnum == SexEnum.NOTFOUND) {
                throw new ServiceException("学生性别非法");
            }
        }

        //判断手机号是否非法
        if (StringUtils.isNotBlank(student.getPhone())) {
            if (!student.getPhone().matches("^1[3-9]\\d{8}$")) {
                throw new ServiceException("手机号非法");
            }
        }
    }

    public PageUtils getStudentsByPage(StudentEntity student) {

//        checkParm(student);
        //参数校验

        Page<StudentEntity> page = new Page<>(student.getPageNo(), student.getPageSize());
        IPage<StudentEntity> studentEntityIPage = this.baseMapper.selectPage(page, buidQueryWrap(student));
        return new PageUtils<>(studentEntityIPage);
    }

    @Override
    public List<StudentEntity> getStudents(StudentEntity student) {


        //1.
        QueryWrapper<StudentEntity> qw = buidQueryWrap(student);
        if (StringUtils.isNotBlank(student.getClassName())) {
            qw.like("class_name", student.getClassName());
        }
//        qw.eq("sex", 1);

        //2.
        LambdaQueryWrapper<StudentEntity> lamdaqw = new LambdaQueryWrapper<StudentEntity>()
                .like(StudentEntity::getName, student.getName())
                .eq(StudentEntity::getSex, 1);
        return this.list(qw);
    }

    @Override
    public Boolean addStudent(StudentEntity student) {
        //参数校验
        checkParm(student);

        return this.save(student);
    }

    @Override
    public Boolean updateStdent(StudentEntity student) {
        //1.校验ID不能为空
        if (Objects.isNull(student.getId())) {
            throw new ServiceException("学生ID不能为空");
        }
        //参数校验
        checkParm(student);

        return this.updateById(student);
    }

    @Override
    public Boolean deleteStudent(List<Integer> studentIds) {
        if (studentIds.isEmpty()) {
            throw new ServiceException("请选择要删除的图书");
        }
//        //1.
//        List<StudentEntity> studentEntities = new ArrayList<>();
//        for (Integer studentId : studentIds) {
//            StudentEntity studentEntity = this.getById(studentId);
//            if(Objects.isNull(studentEntity)){
//                throw new ServiceException("该学生不存在");
//            }
//            studentEntities.add(studentEntity);
//        }
//        //2.
//        List<StudentEntity> studentEntities1 = this.baseMapper.selectByIds(studentIds);

        return this.removeBatchByIds(studentIds);
    }
}
